package logic

import (
	"context"
	"day01/day01rpc/day01rpc"
	"day01/day01rpc/internal/svc"
	"day01/day01rpc/models"
	"day01/pkg"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *RegisterLogic) Register(in *day01rpc.RegisterRequest) (*day01rpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	models.DB.Where("address=?", in.Account).Find(&user)
	result, _ := models.Rdb.Get(l.ctx, "sendsms"+in.Account).Result()
	if result != in.Sendsms {
		return nil, fmt.Errorf("验证码错误")
	}
	users := models.User{
		Account:  in.Account,
		Password: pkg.Md5(in.Password),
	}
	models.DB.Create(&users)
	return &day01rpc.RegisterResponse{
		Id: int64(users.ID),
	}, nil
}
